package com.wnnight.wys.web.config;

import com.wnnight.wys.entity.config.ChargeAccount;
import com.wnnight.wys.groupInterface.AddGroup;
import com.wnnight.wys.groupInterface.AlterGroup;
import com.wnnight.wys.service.config.ChargeAccountService;
import com.wnnight.wys.util.config.MyBatisUtil;
import com.wnnight.wys.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ChargeAccountController
 * @Description: //收款账户Controller
 * @author: LZJ
 * @date: 2022/8/29-23:18
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/v1/config/chargeAccount")
@Slf4j
public class ChargeAccountController {
    @Autowired
    private ChargeAccountService chargeAccountService;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('config:chargeAccount:add')")
    public ResultVo<String> add(@RequestBody @Validated(AddGroup.class) ChargeAccount chargeAccount) {
        chargeAccount.setUpdateTime(MyBatisUtil.getNowTime());
        chargeAccountService.add(chargeAccount);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('config:chargeAccount:update')")
    public ResultVo<String> update(@RequestBody @Validated(AlterGroup.class) ChargeAccount chargeAccount) {
        chargeAccount.setUpdateTime(MyBatisUtil.getNowTime());
        chargeAccountService.update(chargeAccount);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('config:chargeAccount:delete')")
    public ResultVo<String> delete(@PathVariable int id) {
        chargeAccountService.removeById(id);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('config:chargeAccount:list')")
    public ResultVo<ChargeAccount> list() {
        List<ChargeAccount> chargeAccounts = chargeAccountService.getAll();
        return new ResultVo(chargeAccounts);
    }

    @PostMapping("getById/{id}")
    public ResultVo<ChargeAccount> getById(@PathVariable int id) {
        ChargeAccount chargeAccount = chargeAccountService.getById(id);
        return new ResultVo(chargeAccount);
    }
}
